#include "command.h"

void handler_sigup(int client_fd,data msg,char *work_path,MYSQL *mysql,char *username){
    char user_name[55] = {0};
    char password[50] = {0};
    char *token = strtok(msg.args,"&");
    if(token != NULL){
        //获取用户名
        memcpy(user_name,token,strlen(token));
    }
    token = strtok(NULL,"&");
    if(token != NULL){
        //获取密码
        memcpy(password,token,strlen(token));
    }
    //关闭语句的自动提交
    mysql_autocommit(mysql,0);
    //存sql语句
    char query_buf[1024] = {0};
    //存普通打印的日志信息
    char buf_log[1024] = {0};
    //获取
    MYSQL_RES *result;
    MYSQL_ROW row;
    sprintf(query_buf,"select `user_name` from `user`");
    print_sql_to_log("DEBUG",user_name,query_buf);

    //查找所有的user_name
    mysql_query(mysql,query_buf);
    //接收查找的结果
    result = mysql_store_result(mysql);
    //标记是否注册成功
    int flag = 1; 
    if(result == NULL || (mysql_num_rows(result) == 0)){
        flag = -1;
        //直接返回错误码
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        strcpy(username,"");
        //释放掉接收的结果
        mysql_free_result(result);
        return;
    }
    
    while((row = mysql_fetch_row(result))){
        if(strcmp(row[0],user_name) == 0){
            //有同名的用户，返回标记位表示不能注册
            flag = 0;
            send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
            //释放结果
            mysql_free_result(result);
            memset(buf_log,0,sizeof(buf_log));
            sprintf(buf_log,"%s 注册失败,存在同名用户...",user_name);
            log_log("ERROR","SERVER",buf_log);
            //失败修改外部的用户名
            strcpy(username,"");
            //直接返回
            return;
        }
    }
    //释放掉查找的user_name数据
    mysql_free_result(result);

    //说明没有重名的用户，先生成盐值，然后生成hash密码，然后插入到user表中
    char *hashed_password = NULL; //接收生成的哈希密码
    char salt_hex[35] = {0}; //接收盐值
    unsigned char *salt = NULL; //十六进salt
    generate_hash_with_salt(password,&hashed_password,&salt,salt_hex);
    //清空存储sql语句的字符数组
    memset(query_buf,0,sizeof(query_buf));
    //插入到数据库中
    sprintf(query_buf,"insert into `user` values (NULL,'%s','%s','%s')",user_name,hashed_password,salt_hex);
    
    //打印执行的sql语句到日志文件中
    print_sql_to_log("DEBUG",user_name,query_buf);

    int ret = mysql_query(mysql,query_buf);
    if(ret != 0){
        flag = -1;
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        strcpy(username,"");
        //回滚事务
        mysql_rollback(mysql);
        return;
    }
    
    //清空存储sql语句的字符数组
    memset(query_buf,0,sizeof(query_buf));
    //查询当前用户的user_id
    sprintf(query_buf,"select `user_id` from `user` where user_name = '%s'",user_name);
    
    //打印执行的sql语句到日志文件中
    print_sql_to_log("DEBUG",user_name,query_buf);

    //查询并接收数据
    mysql_query(mysql,query_buf);
    //获取查询到的mysql结果并获取第一行
    result = mysql_store_result(mysql);
    if(result == NULL || (mysql_num_rows(result) == 0)){
        flag = -1;
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        strcpy(username,"");
        //释放掉接收的结果
        mysql_free_result(result);
        return;
    }
    //获取result的第一行
    row = mysql_fetch_row(result);
    //往file表中插入一条数据
    memset(query_buf,0,sizeof(query_buf));
    sprintf(query_buf,"insert into `file` values (NULL,'/',%d,'/',1,NULL,0,-1,NULL)",atoi(row[0]));

    //打印执行的sql语句到日志文件中
    print_sql_to_log("DEBUG",user_name,query_buf);

    ret = mysql_query(mysql,query_buf);
    if(ret != 0){
        flag = -1;
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        strcpy(username,"");
        //回滚事务
        mysql_rollback(mysql);
        return;
    }
    //释放掉接收的结果
    mysql_free_result(result);
    //提交事务
    mysql_commit(mysql);
    //开启自动提交
    mysql_autocommit(mysql,1);
    //给客户端发送一个标记位
    send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
    memset(buf_log,0,sizeof(buf_log));
    sprintf(buf_log,"(%s) 注册成功!",user_name);
    strcpy(username,user_name);
    log_log("INFO","SERVER",buf_log);
}